/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Department;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;
import processor.DepartmentFacade;

/**
 *
 * @author Phong
 */
@ManagedBean
@SessionScoped
public class DepartmentMBean {

    @EJB
    private DepartmentFacade departmentFacade;
    private List<Department> departments;
    private List<Department> filteredDepartments;
    private Department selectedDepartment = new Department();
    private String departmentName;

    /**
     * Creates a new instance of Department
     */
    public DepartmentMBean() {
    }

    /**
     * @return the departments
     */
    public List<Department> getDepartments() {
        departments = departmentFacade.getDepartmentAll();
        return departments;
    }

    /**
     * @param departments the departments to set
     */
    public void setDepartments(List<Department> departments) {
        this.departments = departments;
    }

    public void editDepartment(RowEditEvent event) {
        Department de = (Department) event.getObject();
        FacesMessage msg;
        if (departmentFacade.getDepartment(departmentName) == null) {
            if (departmentFacade.editDepartment(de.getDepartmentID(), departmentName)) {
                msg = new FacesMessage("Edited department: ", departmentName + " successfully.");
            } else {
                msg = new FacesMessage("Edited department: ", de.getDepartmentName() + " failed.");
            }
        } else {
            msg = new FacesMessage("Department: ", departmentName + " is existed.");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void cancelEditDepartment(RowEditEvent event) {
        Department de = (Department) event.getObject();
        FacesMessage msg = new FacesMessage("Department Cancelled", de.getDepartmentName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void createDepartment(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            if (departmentFacade.getDepartment(departmentName) == null) {
                if (departmentFacade.createDepartment(departmentName)) {
                    msg = new FacesMessage("Created department: ", departmentName + " successfully.");
                } else {
                    msg = new FacesMessage("Created department: ", departmentName + " failed.");
                }
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Department: ", departmentName + " is existed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void removeDepartment(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            if (departmentFacade.removeDepartment(selectedDepartment.getDepartmentID())) {
                msg = new FacesMessage("Department removed: ", selectedDepartment.getDepartmentName() + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Department removed: ", selectedDepartment.getDepartmentName() + " failed.");
            }
        } catch (Exception ex) {
            msg = new FacesMessage("Error: ", ex.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String getErrorMessage(final String clientId) {
        if (clientId.equals("form:departmentName")) {
            return "Name is required and betwwen 3 to 200 letters";
        } else {
            Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
            if (iter.hasNext()) {
                return iter.next().getDetail(); // or getSummary()
            }
            return "";
        }
    }

    /**
     * @return the filteredDepartments
     */
    public List<Department> getFilteredDepartments() {
        return filteredDepartments;
    }

    /**
     * @param filteredDepartments the filteredDepartments to set
     */
    public void setFilteredDepartments(List<Department> filteredDepartments) {
        this.filteredDepartments = filteredDepartments;
    }

    /**
     * @return the departmentName
     */
    public String getDepartmentName() {
        return departmentName;
    }

    /**
     * @param departmentName the departmentName to set
     */
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    /**
     * @return the selectedDepartment
     */
    public Department getSelectedDepartment() {
        return selectedDepartment;
    }

    /**
     * @param selectedDepartment the selectedDepartment to set
     */
    public void setSelectedDepartment(Department selectedDepartment) {
        this.selectedDepartment = selectedDepartment;
    }
}
